﻿using AutoMapper;
using Benefactor.Models.DtoModels;
using Benefactor.Services;
using Benefactor.Services.Repositories;
using Bigway.Common;
using Bigway.Common.Models.DtoModels;
using Microsoft.AspNetCore.Mvc;

namespace Benefactor.Controllers;
using static Errors;

[ApiController]
[Route("api/benefits_card_product")]
public class BenefitsCardProductController(ICrmebProductRepository crmebProductRepository,
    IAppConfig appConfig,
    IMapper mapper,
    ILogger<BenefitsCardProductController> logger): ControllerBase
{
    [HttpGet]
    public async Task<GetBenefitsCardProductsResponse> GetBenefitsCardProducts(
        [FromQuery(Name = "api_key")] string apiKey,
        [FromQuery(Name = "system_id")] string systemId,
        CancellationToken token = default)
    {
        if (apiKey != appConfig.ApiKey)
        {
            return Unauthorized<GetBenefitsCardProductsResponse>();
        }

        if (systemId != SystemIds.GiftPiker)
        {
            return SystemNotSupported<GetBenefitsCardProductsResponse>(systemId);
        }

        logger.LogInformation("Getting benefits card products");

        var products = await crmebProductRepository.GetAllBenefitsCardStoreProducts(token);

        return new GetBenefitsCardProductsResponse
        {
            Data = products
                .Select(mapper.Map<CrmebProductInfo>)
        };
    }
}